package com.w.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;



public class TankFrame extends Frame{
    public static final int GAME_WIDTH = 800, GAMT_HEIGHT=800;


    public TankFrame(){
      //构造方法
        setSize(GAME_WIDTH,GAMT_HEIGHT);
        setResizable(false);
        setTitle("tank war");
        setVisible(true);
        addKeyListener(new MykeyListener());
        addWindowListener(
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                }
        );

    }



    @Override
    public void paint(Graphics g) {

    for(int i=0;i<GameControl.getInstance().size();i++){
        GameControl.getInstance().get(i).paint(g);
    }
        GameControl.getInstance().deletecoll();

    }

    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if(offScreenImage == null){
            offScreenImage = this.createImage(GAME_WIDTH, GAMT_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        g.setColor(Color.BLACK);
        gOffScreen.fillRect(0,0,GAME_WIDTH, GAMT_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage,0, 0, null);

    }

    public class MykeyListener extends KeyAdapter {
        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;
        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key){
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;

                default:
                    break;
            }
            setMainTankDir();


        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();

            switch (key){
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_SPACE:
                    System.out.println(GameControl.getInstance().maintank == null);
                    GameControl.getInstance().maintank.fs.fire(GameControl.getInstance().maintank);
                    break;
                default:
                    break;
            }
            setMainTankDir();

        }

        private void setMainTankDir(){
            if(!GameControl.getInstance().maintank.live) return;
            if(!bL && !bU && !bR && !bD)
                GameControl.getInstance().maintank.setMoving(false);
            else{
                    GameControl.getInstance().maintank.setMoving(true);
                    if(bD) GameControl.getInstance().maintank.setDir(Dir.DOWN);
                    if(bU) GameControl.getInstance().maintank.setDir(Dir.UP);
                    if(bL) GameControl.getInstance().maintank.setDir(Dir.LEFT);
                    if(bR) GameControl.getInstance().maintank.setDir(Dir.RIGHT);

            }

        }

    }


}
